<template>
  <customDrawer title="请选择机构" subtitle="当前系统所有机构" ref="customDrawer" @confirm="confirm">
    <template #content>
      <treeOffice ref="treeOffice" @click="handleTreeClick" :defaultActived="defaultActived" :checkable="checkable"/>
    </template>
  </customDrawer>
</template>

<script>
import treeOffice from '@/components/custom/bus/treeOffice.vue'

export default {
  desc: '组织机构树',
  name: 'treeOfficeDrawer',
  components: {
    treeOffice
  },
  props: {
  },
  created() {
  },
  data() {
    return {
      // 所选中的树节点
      selectedNode: null,
      // 默认选中的节点
      defaultActived: [0],
      // 确认回调函数
      confirmFun: null,
      // 显示复选框
      checkable: false
    }
  },
  methods: {
    /**
     * @Description：显示-单选
     * @Date 2023-10-10
     * @param defaultActived 选中节点
    */
    show(defaultActived, confirmFun) {
      this.defaultActived = [0]
      this.confirmFun = confirmFun
      this.checkable = false
      if (defaultActived) {
        this.defaultActived = defaultActived
      }
      this.selectedNode = null
      this.$refs.customDrawer.show()
    },
    /**
     * @Description：显示-多选
     * @Date 2023-10-10
     * @param defaultActived 选中节点
    */
    showMore(defaultActived, confirmFun) {
      this.defaultActived = [0]
      this.confirmFun = confirmFun
      this.checkable = true
      if (defaultActived) {
        this.defaultActived = defaultActived
      }
      this.selectedNode = null
      this.$refs.customDrawer.show()
    },
    /**
     * @Description：隐藏
     * @Date 2023-10-10
     * @param defaultActived 选中节点
    */
    hide() {
      this.defaultActived = [0]
      this.selectedNode = null
      this.$refs.customDrawer.hide()
    },

    // 确认
    confirm() {
      if (this.checkable) { // 复选
        let checkDataList = this.$refs.treeOffice.getInst().getCheckDataList()
        console.log('checkDataList', checkDataList)
        if (checkDataList.length > 0) {
          this.$emit('confirm', checkDataList)
          if (this.confirmFun) {
            this.confirmFun(checkDataList)
          }
        }
      } else {//单选
        if (this.selectedNode != null) {
          this.$emit('confirm', this.selectedNode)
          if (this.confirmFun) {
            this.confirmFun(this.selectedNode)
          }
        }
      }
      //getCheckNodeList
      this.hide()
    },

    // 树选择后
    handleTreeClick(data) {
      this.selectedNode = data
    }
  },
  watch: {
  },
  computed: {
  }
}
</script>

<style lang="scss">

</style>
